﻿///////////////////////////////////////////////////////////////
//                                                           //
// Code from Arcanea Project.                                //
//                                                           //
// Please include this license part to every reproduction    //
//   of code.                                                //
//                                                           //
// This code is under Ms-RL license                          //
//                                                           //
// Please read futher information about the license,         //
//   the project (news and updates) and the                  //
//   contributors on the address :                           //
//     http://www.codeplex.com/Arcanea/                      //
//                                                           //
///////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using Arcanea.NetworkEngine.Abstract;
using Arcanea.NetworkEngine.Concrete;
using Arcanea.NetworkEngine.Abstract.Engines;
using Arcanea.Mediator.FullClasses;
using Arcanea.Mediator.Toolbox;
using Arcanea.BaseEngine.Generics;
using System.Configuration;
using Arcanea.BaseEngine.SqlServer;
using Arcanea.BaseEngine.Interfaces;
using Arcanea.BaseEngine;
using log4net;

namespace Arcanea.UnitTest
{
    class ServeurClientUnitTest
    {
        public static void Launch(ILog logger)
        {

            #region TCPNetworkEngine (Single thread a 2,5 Mo/sec)

            //NetworkEngine 
            INetEngine neServer = new TCPServerNetEngine(12345);

            INetEngine neClientS = new TCPClientNetEngine("127.0.0.1", 12345);

            NetInformationContainer infos = null;
            while (infos == null)
            {
                infos = neServer.ProtocoleEngine.AcceptedConnection();
            }

            TCPClientOfServerNetEngine neClient = new TCPClientOfServerNetEngine(infos);
            FullPacket fp = FullToolbox.Instance.CreateFullPacket("personnagehumain");
            fp["login"] = "Toto";
            fp["password"] = "Tata";

            logger.Fatal("StartBench");

            for (int i = 0; i < 1; i++)
            {
                neClient.Send(fp);
                fp = (FullPacket)neClientS.Receive();
            }
            logger.Fatal("StopBench");

            if ((string)fp["login"] != "Toto" || (string)fp["password"] != "Tata")
                logger.Fatal("Binary Serialisation engine not valid");

            neClient.ProtocoleEngine.Close();
            neClientS.ProtocoleEngine.Close();

            #endregion

            #region UDPNetworkEngine (Idem)

            //NetworkEngine 
            INetEngine neServerUDP = new UDPNetEngine("127.0.0.1", 12345, 12344);
            INetEngine neClientUDP = new UDPNetEngine("127.0.0.1", 12344, 12345);

            logger.Fatal("StartBench");

            for (int i = 0; i < 1; i++)
            {
                neServerUDP.Send(fp);
                fp = (FullPacket)neClientUDP.Receive();
            }
            logger.Fatal("StopBench");

            if ((string)fp["login"] != "Toto" || (string)fp["password"] != "Tata")
                logger.Fatal("Binary Serialisation engine not valid");

            neClientUDP.ProtocoleEngine.Close();
            neServerUDP.ProtocoleEngine.Close();

            #endregion

            #region Coherence part
            CoherenceEngine<MySqlAccess> coherenceEngine = new CoherenceEngine<MySqlAccess>();
            coherenceEngine.Initialise(ConfigurationManager.AppSettings["connectionStringMySql"]);
            coherenceEngine.SetCoherence();
            #endregion

            PoolSwitcher<MySqlAccess> pool = new PoolSwitcher<MySqlAccess>();
            pool.Initialise(ConfigurationManager.AppSettings["connectionStringMySql"],5);
            IBaseAccess connection = pool.GetConnection();


            IList<FullPacket> allSelected = ProcessEngine.SelectAll(fp, connection);
            FullPacket temp = ProcessEngine.SelectOne(fp, connection);
            temp = ProcessEngine.SelectOneException(fp, connection);
            
            FullPacket setter = fp.CloneEmpty();
            setter["privilege"] = 30;

            int res = ProcessEngine.UpdateAll(fp, setter, connection);

            fp["privilege"] = null;

            ProcessEngine.DeleteAll(fp, connection);
            ProcessEngine.InsertAll(fp, connection);

            connection.Close();
            

        }

        private static bool Comparer(byte[] a, byte[] b)
        {
            if (a == null && b == null) return true;
            if (a == null) return false;
            if (b == null) return false;

            if (a.Length != b.Length) return false;

            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] != b[i]) return false;
            }
            return true;
        }
    }
}
